/* eslint-disable no-var */
'use strict';

const Controller = require('egg').Controller;

class ListController extends Controller {
  async index() {
    const { ctx } = this;
    var db = this.app.mongodb;
    var res = await db.collection('article').find().toArray();
    var url = ctx.origin + '/public';
    if (res.length > 0) {
      res.forEach(item => {
        item.imgSrc = url + item.imgSrc;
        item.avatar = url + item.avatar;
        item.headImgSrc = url + item.headImgSrc;
      });
      ctx.body = {
        status: 200,
        res,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        status: 400,
        msg: '数据库输入的参数不合法',
      };
    }
  }
}

module.exports = ListController;
